<?php

if (isset($_GET['showcwd']) && ($_GET['showcwd'] == 'on')) {
  echo getcwd();
}

ini_set("memory_limit", "200M");

require('settings.php');

session_name("schoolcmc_session");
session_set_cookie_params(0, '/', '.zsskolska.sk');
session_start();

if (isset($_SESSION['debug_mode']) && ($_SESSION['debug_mode'])) {
  //ini_set('display_errors', 'On');
  //error_reporting(E_ALL);
}

if (isset($_GET['showphpinfo']) && ($_GET['showphpinfo'] == 'on')) {
  phpinfo();
}
// include & construct general base_obj.php
require(PATH_TO_GLOBAL_OBJECTS . 'base_obj.php');
require(PATH_TO_GLOBAL_OBJECTS . 'myDB.php');
require(PATH_TO_GLOBAL_OBJECTS . 'base_data_object.php');
require(PATH_TO_GLOBAL_OBJECTS . 'db_obj.php');
$base_obj = new base_obj();
//include global objects
$objects = $base_obj->getAllObjects(PATH_TO_GLOBAL_OBJECTS);
foreach ($objects as $object) {
  require(PATH_TO_GLOBAL_OBJECTS . $object);
}

//include local objects
$objects = $base_obj->getAllObjects(PATH_TO_OBJECTS);
foreach ($objects as $object) {
  require(PATH_TO_OBJECTS . $object);
}

require('constants.php');

$GLOBALS['errors'] = array();
$GLOBALS['errors_alert'] = array();

$GLOBALS['smarty_functions'] = array(
    'printError' => 'error_obj::smarty_printError',
    //'t' => 'translation_obj::smarty_translation',
    'article' => 'cms_obj::smarty_getArticle',
    'schoolYear' => 'base_obj::smarty_getSchoolYear'
);

$base_obj->smarty();

// set debug mode
if (isset($_GET['dmode']) && ($_GET['dmode'] == 1)) {
  $_SESSION['debug_mode'] = true;
}
if (isset($_GET['dmode']) && ($_GET['dmode'] == 0)) {
  $_SESSION['debug_mode'] = false;
}

$input = str_replace(HTDOCS, "", $_SERVER['REQUEST_URI']);

if (!empty($input)) {
  if (strpos($input, '?') !== false)
    $input = substr($input, 0, strpos($input, '?'));
}

//var_dump($input);

$input = base_obj::setLangFromUrl($input);

//var_dump($_SESSION['lang']);

if (!empty($input) && ($input[strlen($input) - 1] == '/')) {
  $input = substr($input, 0, strlen($input) - 1);
}

//var_dump("wtf");
//var_dump($input);

if (!empty($input)) {
  $input_parts = explode('/', $input);
  
  $GLOBALS['input'] = $input_parts[0];
  unset($input_parts[0]);
  if (!empty($input_parts)) {
    foreach ($input_parts as $param) {
      $GLOBALS['par'][] = $param;
    }
  }
} else {
  $GLOBALS['input'] = INDEX;
}

//var_dump($GLOBALS['input']);
//var_dump($GLOBALS['par']);

require('global_settings.php');

if (isset($_SESSION['timeout'])) {
  if ((time() - $_SESSION['timeout']) > SESSION_EXPIRATION_LIMIT) {
    $user_obj = new user_obj();
    $user_obj->logout();
    header("Location: " . HTDOCS);
    exit;
  } else {
    $_SESSION['timeout'] = time();
  }
}

$GLOBALS['myMemcache'] = new myMemcache($GLOBALS['memcache_settings']);

if (!isset($_SESSION['user']) || empty($_SESSION['user'])) {
  //$user_obj = new user_obj();
  //$_SESSION['user']['user_id'] = 0;
  //$_SESSION['user']['user_type_id'] = 0;
  //$_SESSION['user']['user_area_id'] = $user_obj->getUserArea($_SESSION['user']['user_type_id']);
}

$settings_obj = new settings_obj();
$settings = $settings_obj->getSettingsOfPage();
if (!error_obj::isError($settings) && !empty($settings))
  $_SESSION['pageSettings'] = $settings_obj->getSettingsOfPage();

$GLOBALS['static_pages'] = array(
    'index' => array('title'=>'Úvod','sub'=>null),
    'kontakt' => array('title'=>'Kontakt','sub'=>null),
    'dokumenty-skoly' => array('title'=>'Dokumenty školy','sub'=> null),
    'fotogaleria' => array('title'=>'Fotogaléria','sub'=>null),
    'zmluvy-faktury-objednavky' => array('title'=>'Zmluvy, faktúry, objednávky','sub'=>array(
        'zmluvy' => array('title'=>'Zmluvy','sub'=> null),
        'faktury' => array('title'=>'Faktúry','sub'=> null),
        'objednavky' => array('title'=>'Objednávky','sub'=> null)
    )),
);

if (!empty($_SESSION['pageSettings']['old_page'])) {
  $GLOBALS['static_pages'][$_SESSION['pageSettings']['old_page']] = array('title'=>'Pôvodná stránka','sub'=>null, 'ext' => true);
}

if (!empty($_SESSION['pageSettings']['flag_elearning_enabled'])) {
  $GLOBALS['static_pages']['elearning'] = array('title'=>'Elearning','sub'=>null, 'ext' => true);
}

base_obj::debug("REQUEST", $_REQUEST);
base_obj::debug("GLOBAL INPUT", $GLOBALS['input']);
if (!empty($GLOBALS['par']))
  base_obj::debug("GLOBAL PARAMS", $GLOBALS['par']);
base_obj::debug("SESSION:", $_SESSION);

if (!empty($_REQUEST["tools_key"]) && $_REQUEST["tools_key"] == '133425juraj') {
  switch ($_REQUEST['action']) {
    case 'flush':

      if ($GLOBALS['myMemcache']->flush())
        echo 'flushed';
      break;
  }
}
?>
